﻿using JianZhangTool.config;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace JianZhangTool.statistics
{
    /// <summary>
    /// 航海信息结构
    /// </summary>
    public class GuardInfo
    {
        /// <summary>
        /// UID
        /// </summary>
        public int uid;
        /// <summary>
        /// 用户名
        /// </summary>
        public string uname;
        /// <summary>
        /// 曾用名
        /// </summary>
        public List<string> old_unames = new List<string>();
        /// <summary>
        /// 舰长数
        /// </summary>
        public int jianzhang_num;
        /// <summary>
        /// 提督数
        /// </summary>
        public int tidu_num;
        /// <summary>
        /// 总督数
        /// </summary>
        public int zongdu_num;
        /// <summary>
        /// 深渊次数
        /// </summary>
        public int shenyuan_num;

        /// <summary>
        /// 变化锁
        /// </summary>
        private readonly object change_lock = new object();

        /// <summary>
        /// 从JToken读取航海信息
        /// </summary>
        /// <param name="jt"></param>
        /// <returns></returns>
        public static GuardInfo Parse(JToken jt)
        {
            if (jt["uid"] == null || jt["uname"] == null)
            {
                return null;
            }
            GuardInfo info = new GuardInfo();
            try
            {
                info.uid = jt["uid"].Value<int>();
                info.uname = jt["uname"].ToString();
                if (jt["old_unames"] != null)
                {
                    string[] splits = jt["old_unames"].ToString().Split(',');
                    foreach (string sub in splits)
                    {
                        info.old_unames.Add(sub);
                    }
                }
                if (jt["jianzhang_num"] != null)
                {
                    info.jianzhang_num = jt["jianzhang_num"].Value<int>();
                }
                if (jt["tidu_num"] != null)
                {
                    info.tidu_num = jt["tidu_num"].Value<int>();
                }
                if (jt["zongdu_num"] != null)
                {
                    info.zongdu_num = jt["zongdu_num"].Value<int>();
                }
                if (jt["shenyuan_num"] != null)
                {
                    info.shenyuan_num = jt["shenyuan_num"].Value<int>();
                }
                return info;
            }
            catch (Exception)
            {

            }
            // 在UID和UNAME存在的情况下 默认其他值可有可无
            if (info.uid != 0 && info.uname != null)
            {
                return info;
            }
            return null;
        }

        /// <summary>
        /// 转Json结构
        /// </summary>
        /// <returns></returns>
        public JObject ToJson()
        {
            JObject jo = new JObject();
            jo["uid"] = uid;
            jo["uname"] = uname;
            if (old_unames != null && old_unames.Count > 0)
            {
                StringBuilder ou_arr = new StringBuilder();
                for (int i = 0; i < old_unames.Count; i++)
                {
                    if (i != 0)
                    {
                        ou_arr.Append(",");
                    }
                    ou_arr.Append(old_unames[i]);
                }
                jo["old_unames"] = ou_arr.ToString();
            }
            if (jianzhang_num > 0)
            {
                jo["jianzhang_num"] = jianzhang_num;
            }
            if (tidu_num > 0)
            {
                jo["tidu_num"] = tidu_num;
            }
            if (zongdu_num > 0)
            {
                jo["zongdu_num"] = zongdu_num;
            }
            if (shenyuan_num > 0)
            {
                jo["shenyuan_num"] = shenyuan_num;
            }
            return jo;
        }

        /// <summary>
        /// 上舰
        /// </summary>
        /// <param name="guard_lv">1总督 2提督 3舰长</param>
        public void AddGuard(string uname, int guard_lv)
        {
            lock (change_lock)
            {
                if (!this.uname.Equals(uname))
                {
                    string oldname = this.uname;
                    this.uname = uname;
                    bool find = false;
                    foreach (string on in old_unames)
                    {
                        if (on.Equals(oldname))
                        {
                            find = true;
                            break;
                        }
                    }
                    if (!find)
                    {
                        old_unames.Add(oldname);
                    }
                    // 最多保留5个曾用名
                    if (old_unames.Count > 5)
                    {
                        old_unames.RemoveAt(0);
                    }
                }
                if (guard_lv == 3)
                {
                    jianzhang_num += 1;
                    shenyuan_num += StaticConfig.jianzhang_shenyuan_param;
                }
                else if (guard_lv == 2)
                {
                    tidu_num += 1;
                    shenyuan_num += StaticConfig.tidu_shenyuan_param;
                }
                else if (guard_lv == 1)
                {
                    zongdu_num += 1;
                    shenyuan_num += StaticConfig.zongdu_shenyuan_param;
                }
            }
            Main.instance.GetRoomGuardInfos().MarkChange();
        }

        /// <summary>
        /// 增加一次深渊次数
        /// </summary>
        public void IncShenyuanNum()
        {
            lock (change_lock)
            {
                shenyuan_num += 1;
            }
            Main.instance.GetRoomGuardInfos().MarkChange();
        }

        /// <summary>
        /// 减少一次深渊次数
        /// </summary>
        public void DecShenyuanNum()
        {
            lock (change_lock)
            {
                shenyuan_num -= 1;
            }
            Main.instance.GetRoomGuardInfos().MarkChange();
        }
    }
}
